from usr.dtu.configure import Config
from usr.dtu.clouds.mqttIot import MqttIot
from usr.dtu.clouds.socketIot import SocketIot


class CloudFactory(object):
    config = Config()
    # cloud supported now!
    DEFAULT_CLOUDS = {
        'MQTT': MqttIot,
        'SOCKET': SocketIot
    }

    @classmethod
    def create(cls):
        clouds = []
        clouds_params = cls.config['CLOUDS']
        for params in clouds_params.values():
            print('get cloud params: {}'.format(params))
            cloud_cls = cls.DEFAULT_CLOUDS.get(params.pop('type'))
            if cloud_cls is None:
                raise TypeError(
                    'cloud \"{}\" not supported now!'
                    'maybe you wanna register one using `CloudFactory.register` method.'.format(cloud_cls)
                )
            clouds.append(cloud_cls(**params))
        return clouds

    @classmethod
    def register(cls, name, class_):
        cls.DEFAULT_CLOUDS[name] = class_
